rm(list = ls())

# Set Working Directory to Replication Folder
taskdir 		<- "~/Dropbox/final_hai_perlman_replication/"



setwd(taskdir)
library(tidyverse)
library(lfe)
require(data.table)
library(stringi)


## load survey 2
survey2 <- read_csv(paste0(taskdir, 'input/survey2.csv'))

# Define all DVs
dvs <- c('tax_support' ,'politician_prevent' ,'politician_understand' ,
		'politician_advocate' ,'politician_sympathy'
		,'responsibility_local' ,'responsibility_federal' ,
		'responsibility_international',
		'climatetax_support', 'wildfire_morecommon',
		'carbon_footprint_score')

# m<- felm(paste0("politician_understand",rhs)%>%as.formula, 
# 	data = survey2%>%filter(republican == 1 & politician_democrat==1))

# summary(m,robust = T)
survey2%>%filter(treat == 0 & republican==1)%>%pull(politician_sympathy)%>%mean(na.rm = T)

# survey2%>%filter(republican == 1)%>%pull(cc_happening)%>%mean(na.rm = T)
# survey2%>%filter(democrat == 1)%>%pull(cc_happening)%>%mean(na.rm = T)
# survey2%>%filter(democrat == 0 &republican==0)%>%pull(cc_happening)%>%mean(na.rm = T)
# survey2%>%filter(lost_home==1|know_someone==1)
#-----------------------------------------

## main results (with controls)

# Regression specification
rhs <- ' ~ treat+female+ white+ college  |income|0 | 0'
# Run Regressions
outlist <- list()
for (dv in dvs) {
	fmla <- paste0(dv,rhs)%>%as.formula
	# Politician is a Republican
	mod1 <- felm(fmla, data = survey2%>%filter(politician_democrat==0))
	mod2 <- felm(fmla, data = survey2%>%filter(republican == 1 & politician_democrat==0))
	mod3 <- felm(fmla, data = survey2%>%filter(democrat == 1 & politician_democrat==0))
	mod4 <- felm(fmla, data = survey2%>%filter(republican == 0 & democrat==0 & politician_democrat==0))

	# Politician is a Democrat
	mod5 <- felm(fmla, data = survey2%>%filter(politician_democrat==1))
	mod6 <- felm(fmla, data = survey2%>%filter(republican == 1 & politician_democrat==1))
	mod7 <- felm(fmla, data = survey2%>%filter(democrat == 1 & politician_democrat==1))
	mod8 <- felm(fmla, data = survey2%>%filter(republican == 0 & democrat==0 & politician_democrat==1))

	# save models in list 
	templsit <- list(mod1, mod2, mod3, mod4, mod5, mod6, mod7, mod8)
	outlist[[dv]]  <- templsit
}

save(outlist, 
	file = paste0(taskdir, 'input/processing/regressions_survey2/ols_survey2_controls.RData'))



## main results (without controls)

# Regression specification
rhs_nocontrols <- ' ~ treat |0|0|0'
# Run Regressions
outlist <- list()
for (dv in dvs) {
	fmla <- paste0(dv,rhs_nocontrols)%>%as.formula
	# Politician is a Republican
	mod1 <- felm(fmla, data = survey2%>%filter(politician_democrat==0))
	mod2 <- felm(fmla, data = survey2%>%filter(republican == 1 & politician_democrat==0))
	mod3 <- felm(fmla, data = survey2%>%filter(democrat == 1 & politician_democrat==0))
	mod4 <- felm(fmla, data = survey2%>%filter(republican == 0 & democrat==0 & politician_democrat==0))

	# Politician is a Democrat
	mod5 <- felm(fmla, data = survey2%>%filter(politician_democrat==1))
	mod6 <- felm(fmla, data = survey2%>%filter(republican == 1 & politician_democrat==1))
	mod7 <- felm(fmla, data = survey2%>%filter(democrat == 1 & politician_democrat==1))
	mod8 <- felm(fmla, data = survey2%>%filter(republican == 0 & democrat==0 & politician_democrat==1))

	# save models in list 
	templsit <- list(mod1, mod2, mod3, mod4, mod5, mod6, mod7, mod8)
	outlist[[dv]]  <- templsit
}
save(outlist, 
 	file = paste0(taskdir, 'input/processing/regressions_survey2/ols_survey2_nocontrols.RData'))






## main results for subset with wildfire exposure

# Regression specification
rhs <- ' ~ treat+female+ white+ college|income|0 | 0'
# Run Regressions
outlist <- list()
for (dv in dvs) {
	fmla <- paste0(dv,rhs)%>%as.formula
	# Politician is a Republican
	mod1 <- felm(fmla, data = survey2%>%filter(politician_democrat==0 & (lost_home==1|know_someone==1)))
	mod2 <- felm(fmla, data = survey2%>%filter(republican == 1 & politician_democrat==0 & (lost_home==1|know_someone==1)))
	mod3 <- felm(fmla, data = survey2%>%filter(democrat == 1 & politician_democrat==0 & (lost_home==1|know_someone==1)))
	mod4 <- felm(fmla, data = survey2%>%filter(republican == 0 & democrat==0 & politician_democrat==0 & (lost_home==1|know_someone==1)))

	# Politician is a Democrat
	mod5 <- felm(fmla, data = survey2%>%filter(politician_democrat==1 & (lost_home==1|know_someone==1)))
	mod6 <- felm(fmla, data = survey2%>%filter(republican == 1 & politician_democrat==1 & (lost_home==1|know_someone==1)))
	mod7 <- felm(fmla, data = survey2%>%filter(democrat == 1 & politician_democrat==1 & (lost_home==1|know_someone==1)))
	mod8 <- felm(fmla, data = survey2%>%filter(republican == 0 & democrat==0 & politician_democrat==1 & (lost_home==1|know_someone==1)))
	
	# save models in list 
	templsit <- list(mod1, mod2, mod3, mod4, mod5, mod6, mod7, mod8)
	outlist[[dv]]  <- templsit
}
save(outlist, 
	file = paste0(taskdir, 'input/processing/regressions_survey2/ols_survey2_subset_wildfireexposure.RData'))



## main results with additional control for personal exposure to wildfires

survey2<- survey2%>%mutate(exposure = (lost_home==1|know_someone==1))
# Regression specification
rhs_controls_exposure <- ' ~ treat+exposure +female+ white+ college|income|0 | 0'
# Run Regressions
outlist <- list()
for (dv in dvs) {
	fmla <- paste0(dv,rhs_controls_exposure)%>%as.formula
	# Politician is a Republican
	mod1 <- felm(fmla, data = survey2%>%filter(politician_democrat==0))
	mod2 <- felm(fmla, data = survey2%>%filter(republican == 1 & politician_democrat==0))
	mod3 <- felm(fmla, data = survey2%>%filter(democrat == 1 & politician_democrat==0))
	mod4 <- felm(fmla, data = survey2%>%filter(republican == 0 & democrat==0 & politician_democrat==0))

	# Politician is a Democrat
	mod5 <- felm(fmla, data = survey2%>%filter(politician_democrat==1))
	mod6 <- felm(fmla, data = survey2%>%filter(republican == 1 & politician_democrat==1))
	mod7 <- felm(fmla, data = survey2%>%filter(democrat == 1 & politician_democrat==1))
	mod8 <- felm(fmla, data = survey2%>%filter(republican == 0 & democrat==0 & politician_democrat==1))
	
	# save models in list 
	templsit <- list(mod1, mod2, mod3, mod4, mod5, mod6, mod7, mod8)
	outlist[[dv]]  <- templsit

}
save(outlist, 
	file = paste0(taskdir, 'input/processing/regressions_survey2/ols_survey2_control_wildfireexposure.RData'))





## main results for subset with belief in climate change

# Regression specification
rhs <- ' ~ treat+female+ white+ college|income|0 | 0'
# Run Regressions
outlist <- list()
for (dv in dvs) {
	fmla <- paste0(dv,rhs)%>%as.formula
	# Politician is a Republican
	mod1 <- felm(fmla, data = survey2%>%filter(politician_democrat==0 & cc_happening==1))
	mod2 <- felm(fmla, data = survey2%>%filter(republican == 1 & politician_democrat==0 & cc_happening==1))
	mod3 <- felm(fmla, data = survey2%>%filter(democrat == 1 & politician_democrat==0 & cc_happening==1))
	mod4 <- felm(fmla, data = survey2%>%filter(republican == 0 & democrat==0 & politician_democrat==0 & cc_happening==1))

	# Politician is a Democrat
	mod5 <- felm(fmla, data = survey2%>%filter(politician_democrat==1 & cc_happening==1))
	mod6 <- felm(fmla, data = survey2%>%filter(republican == 1 & politician_democrat==1 & cc_happening==1))
	mod7 <- felm(fmla, data = survey2%>%filter(democrat == 1 & politician_democrat==1 & cc_happening==1))
	mod8 <- felm(fmla, data = survey2%>%filter(republican == 0 & democrat==0 & politician_democrat==1 & cc_happening==1))

	# save models in list 
	templsit <- list(mod1, mod2, mod3, mod4, mod5, mod6, mod7, mod8)
	outlist[[dv]]  <- templsit

}
save(outlist, 
	file = paste0(taskdir, 'input/processing/regressions_survey2/ols_survey2_subset_climatebeleif.RData'))



## main results for subset with knowledge of climate change
# Regression specification
rhs <- ' ~ treat+female+ white+ college|income|0 | 0'
# subset of respondents who read about CC or discuss with it with family atleast once a month
survey2<- survey2%>%mutate(knowledge = (discuss_climate_num %in% c(3,4)|read_climate_num %in% c(3,4)))
survey2%>%pull(knowledge)%>%table

# Run Regressions
outlist <- list()
for (dv in dvs) {
	fmla <- paste0(dv,rhs)%>%as.formula
	# Politician is a Republican
	mod1 <- felm(fmla, data = survey2%>%filter(politician_democrat==0 & knowledge==1))
	mod2 <- felm(fmla, data = survey2%>%filter(republican == 1 & politician_democrat==0 & knowledge==1))
	mod3 <- felm(fmla, data = survey2%>%filter(democrat == 1 & politician_democrat==0 & knowledge==1))
	mod4 <- felm(fmla, data = survey2%>%filter(republican == 0 & democrat==0 & politician_democrat==0 & knowledge==1))

	# Politician is a Democrat
	mod5 <- felm(fmla, data = survey2%>%filter(politician_democrat==1 & knowledge==1))
	mod6 <- felm(fmla, data = survey2%>%filter(republican == 1 & politician_democrat==1 & knowledge==1))
	mod7 <- felm(fmla, data = survey2%>%filter(democrat == 1 & politician_democrat==1 & knowledge==1))
	mod8 <- felm(fmla, data = survey2%>%filter(republican == 0 & democrat==0 & politician_democrat==1 & knowledge==1))

	# save models in list 
	templsit <- list(mod1, mod2, mod3, mod4, mod5, mod6, mod7, mod8)
	outlist[[dv]]  <- templsit

}
save(outlist, 
	file = paste0(taskdir, 'input/processing/regressions_survey2/ols_survey2_subset_climateknowledge.RData'))





## combined results for exposure to wildfires
# Regression specification
rhs <- ' ~ treat+female+ white+ college|income|0 | 0'
# Run Regressions
outlist <- list()
for (dv in "wildfire_morecommon") {
	fmla <- paste0(dv,rhs)%>%as.formula
	# Pooled by Politician Party
	mod1 <- felm(fmla, data = survey2)
	mod2 <- felm(fmla, data = survey2%>%filter(republican == 1 ))
	mod3 <- felm(fmla, data = survey2%>%filter(democrat == 1 ))
	mod4 <- felm(fmla, data = survey2%>%filter(republican == 0 & democrat==0 ))

	# save models in list 
	templsit <- list(mod1, mod2, mod3, mod4)
	outlist[[dv]]  <- templsit

}
save(outlist, 
	file = paste0(taskdir, 'input/processing/regressions_survey2/ols_survey2_pooled_wildfiremorecommon.RData'))
























